package com.iot.ammeter.controller.pay;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iot.ammeter.anno.LogAnnotation;
import com.iot.ammeter.dto.web.pay.PayDetailDTO;
import com.iot.ammeter.query.web.pay.DegreeQuery;
import com.iot.ammeter.query.web.pay.PayQuery;
import com.iot.ammeter.responseResult.ResponseResult;
import com.iot.ammeter.service.web.pay.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @ClassName: PayController
 * @Author: Basil
 * @Date: 2022/1/6 上午9:01
 * @Version: 1.0
 * @Description: 充值管理，充值明细，缴费明细列表
 **/
@RestController
@Api(tags = {"充值管理"})
@RequestMapping("/api/")
public class PayController {

    private final PayService payService;

    public PayController(PayService payService) {
        this.payService = payService;
    }


    /**
     * 获取缴费明细列表
     *
     * @param page     页数
     * @param pageSize 每页数量
     * @param payQuery 查询条件
     * @return 充值列表json数据
     */
    @ApiOperation("获取缴费明细列表")
    @LogAnnotation(module = "listPayDetail", operator = "获取缴费明细列表")
    @PostMapping("/electricity/pay/list/{page}/{pageSize}")
    public ResponseResult listPayDetail(@PathVariable int page, @PathVariable int pageSize, @RequestBody(required = false) PayQuery payQuery) {
        if (page < 0 || pageSize < 5) {
            return ResponseResult.FAILED("页码参数有误");
        }
        IPage<PayDetailDTO> iPage = payService.payDetailList(page, pageSize, payQuery);
        return iPage.getRecords().size() > 0 ? ResponseResult.SUCCESS(iPage) : ResponseResult.SUCCESS("无数据");
    }

    /**
     * 给app端用户充值
     *
     * @param appUserId      app用户id
     * @param rechargeAmount 充值金额
     * @return int
     */
    @ApiOperation("给app端用户充值")
    @LogAnnotation(module = "recharge", operator = "给app端用户充值")
    @PostMapping("/recharge/{appUserId}")
    public ResponseResult recharge(@PathVariable String appUserId, @RequestBody BigDecimal rechargeAmount) {
        int result = 0;
        if (Objects.nonNull(appUserId) && Objects.nonNull(rechargeAmount)) {
            result = payService.rechargeForAppUser(appUserId, rechargeAmount);
        }
        return result > 0 ? ResponseResult.SUCCESS("充值成功") : ResponseResult.FAILED("充值失败");
    }

    /**
     * 获取用电详情
     *
     * @param degreeQuery 查询条件
     * @return map
     */
    @ApiOperation("查看电量详情")
    @LogAnnotation(module = "degreeDetails", operator = "查看电量详情")
    @PostMapping("/degree_details")
    public ResponseResult degreeDetails(@RequestBody DegreeQuery degreeQuery) {
        return ResponseResult.SUCCESS(payService.degreeDetails(degreeQuery));
    }
}
